package com.lanqiao.gxnushop.service;

import com.lanqiao.gxnushop.entity.CartItems;
import com.lanqiao.gxnushop.mapper.CartItemsMapper;
import com.lanqiao.gxnushop.util.UUIdUtil;
import com.lanqiao.gxnushop.valueObject.CarProduct;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Schema(description = "购物车服务")
@Service
public class CartItemsService {
    @Autowired
    private CartItemsMapper cartItemsMapper;

    @Autowired
    private ProductsService productsService;
    @Schema(description = "添加购物车,判断商品存不存在，存在才能放入购物车")
    public boolean add(CartItems cartItems)
    {
        if(productsService.findById(cartItems.getProductId())!=null)
        {
            cartItems.setCartItemId(UUIdUtil.getUserId());
            return cartItemsMapper.add(cartItems);
        }else return false;
    }

    @Schema(description = "删除购物车")
    public boolean delete(String cartItemId)
    {
        return cartItemsMapper.delete(cartItemId);
    }

    @Schema(description = "根据买家id查询购物车")
    public ArrayList<CarProduct> findByBuyerId(String buyerId)
    {
        return cartItemsMapper.findByBuyerId(buyerId);
    }

   /* @Schema(description = "根据购物车商品id查询购物车")
    public CartItems findById(String cartItemId)
    {
        return cartItemsMapper.findById(cartItemId);
    }*/

    @Schema(description = "更新购物车")
    public boolean update(CartItems cartItems)
    {
        return cartItemsMapper.update(cartItems);
    }

}
